package cn.holy.app.xcddt.ui.activity.record.livedata

import androidx.lifecycle.LiveData
import android.content.Context
import android.location.LocationManager
import com.amap.api.location.AMapLocation
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import javax.inject.Inject


/**
 * Created by 69568 on 2018/2/28.
 */
class LocationLiveData @Inject constructor(context: Context) : LiveData<AMapLocation>() {
    private var sInstance: LocationLiveData? = null
    private val locationManager: LocationManager? = null

    private var locationClient: AMapLocationClient? = null
    private var mLocationListener = AMapLocationListener { aMapLocation ->
        run {
            postValue(aMapLocation)
        }
    }

    override fun onActive() {
        super.onActive()
        locationClient?.startLocation()
    }
    override fun onInactive() {
        super.onInactive()
        locationClient?.stopLocation()
    }

    init {
        locationClient = AMapLocationClient(context)
        locationClient?.setLocationListener(mLocationListener)
        val option = AMapLocationClientOption()

        option.isNeedAddress = true
        option.locationMode = AMapLocationClientOption.AMapLocationMode.Hight_Accuracy;
        option.isMockEnable = true
        locationClient?.setLocationOption(option)
    }

}